package com.prs.crm.service.fs.impl;

import java.io.File;

import com.prs.crm.context.Attach;
import com.prs.crm.context.Constrains;
import com.prs.crm.context.FileUtil;
import com.prs.crm.dao.fs.DirectEntryDao;
import com.prs.crm.domain.Attachment;
import com.prs.crm.domain.fs.Detail;
import com.prs.crm.domain.fs.DirectEntry;
import com.prs.crm.service.fs.DetailService;
import com.prs.crm.service.fs.DirectEntryService;
import com.prs.crm.service.impl.BaseServiceImpl;

public class DirectEntryServiceImpl extends BaseServiceImpl<DirectEntry,DirectEntryDao> implements DirectEntryService{
	private DetailService detailService;
	
	@Override
	public void submitDirectEntry(DirectEntry directEntry, File file,
			String fileName) {

		Attach dAttach = FileUtil.getInstance().saveFile(file, fileName,"",Constrains.FS_ITEMS_DIRECTENTRY);
		if(dAttach.getSrc()!= null) {
			directEntry.setEntry_file(dAttach.getSrc());
		}
		directEntry.setState(Constrains.TASK_AUDITING);
		this.getDao().save(directEntry);		
		Double sum = this.getDetailService().getBalanceByAccount(directEntry.getBankAccount());
		Detail detial = new Detail();	
		detial.setDirectEntry(directEntry);
		detial.setIncome(directEntry.getMoney());
		detial.setPay(0.00);
		detial.setBalance(directEntry.getMoney() + sum);
		detial.setBankAccount(directEntry.getBankAccount());
		detial.setItem(Constrains.FS_ITEMS_DIRECTENTRY);
		
		this.getDetailService().save(detial);
	}

	public void setDetailService(DetailService detailService) {
		this.detailService = detailService;
	}

	public DetailService getDetailService() {
		return detailService;
	}

}
